package com.imonsoft.sqm.service.excel.PartExcel;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.imonsoft.common.constant.ConstantDict;
import com.imonsoft.sqm.pojo.basedata.Dictionary;
import com.imonsoft.sqm.pojo.basedata.Part;
import com.imonsoft.sqm.pojo.basedata.Supplier;
import com.imonsoft.sqm.service.basedata.DictionaryService;
import com.imonsoft.sqm.service.basedata.PartService;
import com.imonsoft.sqm.service.basedata.SupplierService;
import com.imonsoft.sqm.web.excel.PartExcel.PartImportDto;
@Service("partExcelService")
public class PartExcelServiceImpl implements PartExcelService {
	@Autowired
	private DictionaryService dictionaryService;
	@Autowired
	private PartService partService;
	@Autowired
	private SupplierService supplierService;

	@Override
	public List<String> txCheckData(PartImportDto partImportDto, Map<Integer, String> setCode) {
		List<String> messages = new ArrayList<String>();
		List<Supplier> supplierList = supplierService.queryAll();
		List<Dictionary> partTypeList = dictionaryService.findDictionaryByType(ConstantDict.PART_PART_TYPE);
		List<Dictionary> productTypeList = dictionaryService.findDictionaryByType(ConstantDict.PART_PRODUCT_TYPE);
		if (supplierList == null || supplierList.size() <= 0) {
			messages.add("系统中不存在供应商，请先维护供应商数据");
		}
		if (partTypeList == null || partTypeList.size() <= 0) {
			messages.add("系统中不存在零件类型，请先维护零件类型数据");
		}
		if (productTypeList == null || productTypeList.size() <= 0) {
			messages.add("系统中不存在产品类型，请先维护产品类型数据");
		}
		if (StringUtils.isEmpty(partImportDto.getCode())) {
			messages.add("第[" + partImportDto.getRow() + "]行零件编号不能为空!");
		} else {
			Part part = new Part();
			part.setCode(partImportDto.getCode());
			List<Part> checkHave = partService.findList(part);
			if (checkHave != null && checkHave.size() > 0) {
				messages.add("第[" + partImportDto.getRow() + "]行零件编号系统已经存在!");
			}
		}
		if (setCode != null) {
			@SuppressWarnings("rawtypes")
			Iterator it = setCode.entrySet().iterator();
			while (it.hasNext()) {
				@SuppressWarnings("rawtypes")
				Map.Entry e = (Map.Entry) it.next();
				if (partImportDto.getCode() != null && StringUtils.isNotBlank(partImportDto.getCode()) && partImportDto.getCode().equals(e.getValue()) && partImportDto.getRow() != Integer.parseInt(e.getKey() + "")) {
					messages.add("第[" + partImportDto.getRow() + "]行与" + "第[" + e.getKey() + "]行" + "零件编号重复!");
					break;
				}
			}
		}
		if (StringUtils.isEmpty(partImportDto.getName())) {
			messages.add("第[" + partImportDto.getRow() + "]行零件名称不能为空!");
		}
		if (StringUtils.isEmpty(partImportDto.getPartTypeName())) {
			messages.add("第[" + partImportDto.getRow() + "]行零件类型不能为空!");
		} else {
			if (partTypeList != null && partTypeList.size() > 0) {
				for (Dictionary d : partTypeList) {
					if (d.getName().equals(partImportDto.getPartTypeName())) {
						partImportDto.setPartType(d.getCode());
						break;
					}
				}
			}
			if (partImportDto.getPartType() == null) {
				messages.add("第[" + partImportDto.getRow() + "]行零件类型系统中不存在!");
			}
		}
		if (StringUtils.isEmpty(partImportDto.getProductTypeName())) {
			messages.add("第[" + partImportDto.getRow() + "]行产品类型不能为空!");
		} else {
			if (productTypeList != null && productTypeList.size() > 0) {
				for (Dictionary d : productTypeList) {
					if (d.getName().equals(partImportDto.getProductTypeName())) {
						partImportDto.setProductType(d.getCode());
						break;
					}
				}
			}
			if (partImportDto.getProductType() == null) {
				messages.add("第[" + partImportDto.getRow() + "]行产品类型系统中不存在!");
			}
		}
		if (StringUtils.isEmpty(partImportDto.getSupplierCode())) {
			messages.add("第[" + partImportDto.getRow() + "]行供应商编号不能为空!");
		} else {
			if (supplierList != null && supplierList.size() > 0) {
				for (Supplier s : supplierList) {
					if (s.getCode().equals(partImportDto.getSupplierCode())) {
						partImportDto.setSupplierId(s.getId());
						partImportDto.setSupplierName(s.getName());
						break;
					}
				}
			}
			if (partImportDto.getSupplierId() == null || partImportDto.getName() == null) {
				messages.add("第[" + partImportDto.getRow() + "]行供应商系统中不存在!");
			}
		}
		return messages;
	}
}
